package com.company.management.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.company.management.dto.UserDto;
import com.company.management.modal.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ScoreTask {
    @Resource
    UserService userService;


    //@Scheduled(cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 0 0 1 * ?") // 每月第一天0点执行
    public void performTask() {
        log.info("=======================定时任务执行中==========================");
        List<User> userList = new ArrayList<>();
       userService.listAll().stream().forEach(userdto -> {
           User user = new User();
           userdto.setScore(100);
           BeanUtils.copyProperties(userdto, user);
           userList.add(user);
       });
        log.info("=======================批量更新用户：{}==========================", userList);
        if (CollectionUtils.isNotEmpty(userList)) {
            userService.batchUpdateUsers(userList);
        }
    }
}
